$(document).ready(function() {
	initTemplate();
	get_faqs();
	$("#save_faq").click(function() {
		save_faq();
	});
	$("#nav_back").click(function() {
		back();
	});
	$("#nav_next").click(function() {
		nextStep();
	});

});
/**
 * 初始化模板
 */
function initTemplate() {
	shouldShowbottom();
};
//判断编辑状态还是展示状态
function shouldShowbottom() {
		var showBottom = $("#static").val();
		var container = $("#container");
		var height = 0;
		if (showBottom == '') {
			height = $(window).height() - 47;
		} else {
			height = $(window).height();
			//改变#div_form的bottom为0
			$("#div_form").css('bottom', '0px');
		};
		container.height(height);
}
//返回上个界面
function back() {
	window.history.back();
}

function nextStep() {
	var site_id = $('#site_id').val();
	var t_id = $('#t_id').val();
	var color = $('#color').val();
	window.location.href = "http://182.92.111.163/baisi/webroot/index.php/generate_html/index?site_id="+site_id+"&t_id="+t_id+"&color="+color;
	// window.location.href = "http://127.0.0.1/baisi/webroot/index.php/generate_html/index?site_id=" + site_id + "&t_id=" + t_id + "&color=" + color;
}


function save_faq() {
	var site_id = $("#site_id").val();
	var problem = $("#problem").val();
	var star = 5;
	if (site_id && problem) {
		var url = "http://182.92.111.163/baisi/webroot/index.php/faq/save_faq";
		$.ajax({
			type: "POST",
			url: url,
			data: "site_id=" + site_id + "&problem=" + problem,
			async: false,
			success: function(data) {
				alert(data);
				get_faqs()
			},
			error: function(data) {
				alert("提问失败");
			}
		});
	} else {
		alert("提问内容不能为空");
		return false;
	}
}

function get_faqs() {
	var site_id = $("#site_id").val();
	if (site_id) {
		var url = "http://182.92.111.163/baisi/webroot/index.php/faq/get_faqs";
		$.ajax({
			type: "POST",
			url: url,
			data: "site_id=" + site_id,
			async: false,
			success: function(data) {
				if (data != false) {
					var list = $("#list");
					list.html('');
					var dataObj = eval("(" + data + ")");
					for (var i = 0; i < dataObj.length; i++) {
						list.append("<div class='q_item'><div id='question'>" + dataObj[i].problem + "</div><div id='answer'>" + dataObj[i].answer + "</div></div>");
					}
				} else {
					$("#list").html('');
					$("#list").append("<div class='q_item'><div id='question'>当前没有问题</div><div id='answer'>&nbsp;</div></div>");
				}
			},
			error: function(data) {
				$("#list").append("<div class='q_item'><div id='question'>当前没有问题</div><div id='answer'>&nbsp;</div></div>");
			}
		});
	} else {
		$("#list").append("<div class='q_item'><div id='question'>你们的客服电话是多少？</div><div id='answer'>北京客服电话：110-12345678</div></div>");
	}
}
